﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Class_DTO;

namespace Class_DAO
{
    public  class BangGiaDAO
    {
        public DataTable getDanhSachBG()
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("getBangGia", cn);
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;

        }

        public DataTable getBangGia_theoma(int pMa)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlDataAdapter ad = new SqlDataAdapter("[getBangGia_theoma]", cn);
            SqlCommand cmd = new SqlCommand("getBangGia_theoma", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@malqc", SqlDbType.Int);
            cmd.Parameters["@malqc"].Value = pMa;
            ad.SelectCommand = cmd;
            DataTable dt = new DataTable();
            ad.Fill(dt);
            cn.Close();
            return dt;
        }

        public void ThemBangGia_DAO(BangGiaDTO bgDTO)
        {
            DataProvider.GetConnection();
            SqlConnection cn = DataProvider.GetConnection();
            cn.Open();
            SqlCommand cmd = new SqlCommand("insertBangGia", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@malqc", SqlDbType.Int);
            cmd.Parameters.Add("@tenloai", SqlDbType.NVarChar);
            //cmd.Parameters.Add("@soluongkt", SqlDbType.Int);
            cmd.Parameters.Add("@sizekt", SqlDbType.Int);
            cmd.Parameters.Add("@trangso", SqlDbType.Int);
            cmd.Parameters.Add("@giatien", SqlDbType.Float);
            cmd.Parameters.Add("@ghichu", SqlDbType.NVarChar);
            cmd.Parameters["@malqc"].Direction = ParameterDirection.Output;


            cmd.Parameters["@tenloai"].Value = bgDTO.Tenloai ;
            //cmd.Parameters["@soluongkt"].Value = bgDTO.Soluong ;
            cmd.Parameters["@sizekt"].Value = bgDTO.Size ;
            cmd.Parameters["@trangso"].Value = bgDTO.Trangso ;
            cmd.Parameters["@giatien"].Value = bgDTO.Giatien ;
            cmd.Parameters["@ghichu"].Value = bgDTO.Ghichu;

            cmd.ExecuteNonQuery();

            int ml = Convert.ToInt32(cmd.Parameters["@malqc"].Value);
            bgDTO.Maloai = ml;
            cn.Close();
        }
        // sua lam theo mo hinh khac
        public void sua(int ma, string ten, int size, int trang, float gt, string ghichu)
        {
            SqlParameter[] sp = new SqlParameter[6];
            sp[0] = new SqlParameter("@malqc", ma);
            sp[1] = new SqlParameter("@tenloai", ten);
            //sp[2] = new SqlParameter("@soluongkt", sl);
            sp[2] = new SqlParameter("@sizekt", size);
            sp[3] = new SqlParameter("@trangso", trang);
            sp[4] = new SqlParameter("@giatien", gt);
            sp[5] = new SqlParameter("@ghichu", ghichu);

            DataProvider.proc("updateBangGia", sp);
        }
        public void xoa(int s1)
        {
            SqlParameter[] sp = new SqlParameter[1];
            sp[0] = new SqlParameter("@malqc", s1);
            DataProvider.proc("deleteBangGia", sp);
        }
    }
}
